package edu.osu.cse.c651.cryptochess;

import java.util.ArrayList;

/**
 * The King
 * 
 * @author Team Pawned
 * 
 */
public class King implements Piece {

	/**
	 * The Color of this King
	 */
	private Color color;

	/**
	 * The number of times this King has moved
	 */
	private int moveCount;

	/**
	 * Constructor
	 * 
	 * @param color
	 *            the Color of this King
	 */
	public King(Color color) {
		this.color = color;
		moveCount = 0;
	}

	/**
	 * 
	 */
	public boolean legalMove(int x1, int y1, int x2, int y2) {
		if ((x1 == x2) && (y1 == y2)) {
			return false;
		}
		if ((Math.abs(x1 - x2) <= 1) && (Math.abs(y1 - y2) <= 1)) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	public Color color() {
		return color;
	}

	/**
	 * 
	 */
	public PieceName name() {
		return PieceName.King;
	}

	/**
	 * 
	 */
	public void movesPlusOne() {
		moveCount += 1;
	}

	/**
	 * 
	 */
	public int moveCount() {
		return moveCount;
	}

	/**
	 * 
	 */
	public ArrayList<Coordinate> possibleMoves(ChessBoard board, int x, int y) {
		ArrayList<Coordinate> moves = new ArrayList<Coordinate>();

		if ((x + 1 < 8)
				&& (board.getPiece(x + 1, y) == null || board
						.getPiece(x + 1, y).color() != this.color)) {
			Coordinate move = new Coordinate(x + 1, y);
			moves.add(move);
		}
		if ((x - 1 >= 0)
				&& (board.getPiece(x - 1, y) == null || board
						.getPiece(x - 1, y).color() != this.color)) {
			Coordinate move = new Coordinate(x - 1, y);
			moves.add(move);
		}
		if ((x + 1 < 8 && y + 1 < 8)
				&& (board.getPiece(x + 1, y + 1) == null || board.getPiece(
						x + 1, y + 1).color() != this.color)) {
			Coordinate move = new Coordinate(x + 1, y + 1);
			moves.add(move);
		}
		if ((x + 1 < 8 && y - 1 >= 0)
				&& (board.getPiece(x + 1, y - 1) == null || board.getPiece(
						x + 1, y - 1).color() != this.color)) {
			Coordinate move = new Coordinate(x + 1, y - 1);
			moves.add(move);
		}
		if ((x - 1 >= 0 && y + 1 < 8)
				&& (board.getPiece(x - 1, y + 1) == null || board.getPiece(
						x - 1, y + 1).color() != this.color)) {
			Coordinate move = new Coordinate(x - 1, y + 1);
			moves.add(move);
		}
		if ((x - 1 >= 0 && y - 1 >= 0)
				&& (board.getPiece(x - 1, y - 1) == null || board.getPiece(
						x - 1, y - 1).color() != this.color)) {
			Coordinate move = new Coordinate(x - 1, y - 1);
			moves.add(move);
		}
		if ((y - 1 >= 0)
				&& (board.getPiece(x, y - 1) == null || board
						.getPiece(x, y - 1).color() != this.color)) {
			Coordinate move = new Coordinate(x, y - 1);
			moves.add(move);
		}
		if ((y + 1 < 8)
				&& (board.getPiece(x, y + 1) == null || board
						.getPiece(x, y + 1).color() != this.color)) {
			Coordinate move = new Coordinate(x, y + 1);
			moves.add(move);
		}

		return moves;
	}
}
